﻿using AutoMapper;
using BaseData.API.Read.Applications.Commands;
using BaseData.API.Read.Dtos;
using BaseData.Domain;
using BaseData.ErrorCode;
using BaseData.Infrastructure.Interfaces;
using MediatR;
using System.Collections.Generic;

namespace BaseData.API.Read.Applications.CommandHandlers
{
    /// <summary>
    /// 角色管理查询命令
    /// </summary>
    public class RoleQueryCommandHandler : IRequestHandler<RoleQueryCommand, ApiResult<ApiPaging<List<RoleDTO>>>>
    {
        private readonly IBaseRepository<Role> roleRep;
        private readonly IBaseRepository<RolePermissionRelation> rolePermissionRep;
        private readonly IBaseRepository<Permission> permissionRep;
        private readonly IMapper mapper;

        public RoleQueryCommandHandler(IBaseRepository<Role> roleRep,IBaseRepository<RolePermissionRelation> rolePermissionRep,
            IBaseRepository<Permission> permissionRep,IMapper mapper)
        {
            this.roleRep = roleRep;
            this.rolePermissionRep = rolePermissionRep;
            this.permissionRep = permissionRep;
            this.mapper = mapper;
        }
        public Task<ApiResult<ApiPaging<List<RoleDTO>>>> Handle(RoleQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<List<RoleDTO>>> result=new ApiResult<ApiPaging<List<RoleDTO>>>();
            result.Code=ApiEnum.查询成功;
            result.Message=ApiEnum.查询成功.ToString();

            //var list = from a in roleRep.GetAll()
            //           join b in rolePermissionRep.GetAll()
            //           on a.RoleId equals b.RoleId
            //           join c in permissionRep.GetAll()
            //           on b.PermissionId equals c.PermissionId
            //           select new RoleDTO
            //           {
            //               RoleId = a.RoleId,
            //               RoleName=a.RoleName,
            //               CreateTime=a.CreateTime,
            //               Status=a.Status,
            //               PermissionId=b.PermissionId,
            //               PermissionName=c.PermissionName,
            //               PermissionUrl=c.PermissionUrl,
            //           };
            var query = roleRep.GetAll();
            if(!string.IsNullOrEmpty(request.RoleName))
            {
                query = query.Where(x => x.RoleName.Contains(request.RoleName));
            }
            var TotalCount=query.Count();
            var PageCount=(int)Math.Ceiling((double)TotalCount/request.PageSize);

            var list=query.OrderBy(x=>x.RoleId).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToList();

            //转换
            var data=mapper.Map<List<RoleDTO>>(list);
            foreach (var item in data)
            {
                var permissionIds = rolePermissionRep.GetAll().Where(x => x.RoleId == item.RoleId).Select(x => x.PermissionId).ToList();
                var permissionName=permissionRep.GetAll().Where(x=>permissionIds.Contains(x.PermissionId)).Select(x=>x.PermissionName).ToList();
                item.PermissionId = permissionIds;
                item.PermissionName=permissionName;
            }
            ApiPaging<List<RoleDTO>> paging = new ApiPaging<List<RoleDTO>>();
            paging.TotalCount=TotalCount;
            paging.PageCount=PageCount;
            paging.Data=data;

            result.Data=paging;

            return Task.FromResult(result);
        }
    }
}
